package cn.wolfcode.shop.cloud.util;

import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil {


    public static void addCookie(String cookieName, String cookieValue, int expireTime, HttpServletResponse response){
        Cookie cookie = new Cookie(cookieName,cookieValue);
        //作用域
        cookie.setDomain("localhost");
        //生效滤镜
        cookie.setPath("/");
        //过期时间
        cookie.setMaxAge(expireTime);
        response.addCookie(cookie);
    }

    public static String getCookieValue(HttpServletRequest request, String cookieName) {
        //获取到请求中所有的cookie
        Cookie[] cookies = request.getCookies();
        if(cookies != null && cookies.length > 0){
            for (Cookie cookie : cookies){
                if(cookie.getName().equals(cookieName)){
                    return cookie.getValue();
                }
            }
        }
        return null;
    }
}
